#include "depothandler.h"
#include <QVariant>
#include <QDate>

#define DEPOT_VALSYMBOLS_CONFIG_KEY "DEPOT_VALSYMBOLS"

DepotHandler::DepotHandler(QObject *parent,QWidget* wParent):
    DepotView(wParent),DB(parent)
{

}

DepotHandler::DepotHandler(Config *cfg, QObject *parent,QWidget* wParent):
    DepotView(wParent),DB(parent)
{
    if(cfg!=NULL)
        this->cfg=cfg;

    init();
}

DepotHandler::DepotHandler(Stock *s,Config* cfg, QObject *parent,QWidget* wParent):
    DepotView(wParent),DB(parent)
{
    if(cfg)
        this->cfg=cfg;
    //if(s)
    //{
        //tmpStock=s;
        /*if a new stock was added, update the depotview*/
        connect((View*)Global::getStockInstance(),SIGNAL(newValueSignal(QString,QList<QStandardItem*>)),this,SLOT(newStockValue()));
    //}
    init();
}

DepotHandler::~DepotHandler()
{

}

/*adds a depot to the database and to the map*/
void DepotHandler::addDepot(const QString &depotName, const QString& liquidity, const QString& standardProvision, int type)
{
    QStringList tableVals;

    //make a list with all depot values
    tableVals<<"1"<<depotName<<liquidity<<liquidity<<standardProvision<<QString("%1").arg(type);

    /*Add new depot to map*/
    Depot* tmp=new Depot(depotName,QStringList(),tableVals);
    tmp->init(cfg,STORE_DEPOT_STD_VALUES);                                       //init the tradehandler
    depots.insert(depotName,tmp);

    show();                                                                      //update depotview
}

/*overloaded function*/
void DepotHandler::addDepot(const QString &depotName,qreal liquidity,qreal standardProvision, int type)
{
    addDepot(depotName,QString("%1").arg(liquidity),QString("%1").arg(standardProvision),type);
}

void DepotHandler::init()
{
    QStringList depotNames;

    /*add the standard depot table with standard fields*/
    addTable(DEPOT_TABLE_NAME,Depot::getStdValSymbols());

    /*load all depot names from the standard depot table and save it in depotNames*/
    getColumn(&depotNames,DEPOT_TABLE_NAME_FIELD,DEPOT_TABLE_NAME,false);

    /*go through the depot name list*/
    for(int i=0;i<depotNames.count();i++)
    {
        qDebug()<<"DepotHandler::init(): "<<"Loading "<<depotNames.at(i);

        /*add depot name with the depot values to depot map*/
        Depot* tmp=new Depot(depotNames.at(i),QStringList());
        tmp->init(cfg);

        depots.insert(depotNames.at(i),tmp);
    }
}

/*returns the depot wit name depotName*/
Depot *DepotHandler::getDepot(const QString &depotName) const
{
    return depots.value(depotName);
}

/*returns all available depot names*/
QStringList DepotHandler::getAllDepotNames()const
{
    return depots.keys();
}

/*returns all available stock names
QStringList DepotHandler::getStockNames()const
{
    return tmpStock->getStockNames();
}*/

/*SLOT: set the current shown depot with the given index*/
void DepotHandler::viewDepotChanged(int index)
{
    qDebug()<<"Depot changed to "<<getAllDepotNames().at(index);
    currentViewedDepot=depots[getAllDepotNames().at(index)];
}

/*void DepotHandler::changedStandardValue(const QString &depotName, const QString &standardValueField,const QString& value)
{
    qDebug()<<"DepotHandler::changedStandardValue(): "<<"updating value "<<standardValueField<<"="<<value<<" in "<<depotName;
    updateValue(DEPOT_TABLE_NAME,standardValueField,value,QString(DEPOT_TABLE_NAME_FIELD)+"='"+depotName+"'");
}*/

/*add a stock with the given stock name to the depot with the given depot name*/
void DepotHandler::addStockToDepot(const QString &depotName, const QString &stockName,int flag)
{
    qDebug()<<"DepotHandler::addStockToDepot(): "<<"Adding stock "<<stockName<<" to depot "<<depotName;
    if(getAllDepotNames().contains(depotName))                              //if depot is available
    {
        Stock* tmp=depots[depotName]->getStocks();                          //get stocks of depot to add stockName
        //tmp->setStaticStockNames(tmpStock->getStockNames());            //update the static stock name list
        //tmp->setStockSymbols(tmpStock->getStockSymbols());              //update the stock symbol list

        //TODO: look for a better way to update these stock values!
        //depots[depotName]->getTradeStocks()->setStaticStockNames(tmpStock->getStockNames());
        //depots[depotName]->getTradeStocks()->setStockSymbols(tmpStock->getStockSymbols());

        if(!tmp->getStockNames().contains(stockName))                       //if stock name is not already in depot
        {
            qDebug()<<"DepotHandler::addStockToDepot(): "<<"Adding...";
            qDebug()<<Global::getStockInstance()->getStocks();
            tmp->addStock(stockName,ADD_STOCK_NAME,Global::getStockInstance()->getStocks());                        //add stock name to map and emit signal to tableview, don't save it to db

            if(flag==NEW_DEPOT_STOCK)                                       //if the stock is new in the depot -> add it to depot table in db
            {
                depots[depotName]->addStock(stockName);                     //add stock to depot table
            }
        }
    }
}
